package cn.iocoder.yudao.module.reading.controller.app.profile;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.reading.controller.app.profile.vo.*;
import cn.iocoder.yudao.module.reading.service.profile.ProfileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * 用户 APP - 个人资料
 *
 * @author 李晨
 */
@Tag(name = "用户 APP - 个人资料")
@RestController
@RequestMapping("/reading/profile")
@Validated
@Slf4j
public class ProfileController {

    @Resource
    private ProfileService profileService;

    @GetMapping("/profile")
    @Operation(summary = "获取用户基本信息")
    @PreAuthenticated
    public CommonResult<ProfileInfoRespVO> getUserProfile() {
        Long userId = getLoginUserId();
        log.info("获取用户基本信息，用户ID: {}", userId);
        
        ProfileInfoRespVO result = profileService.getUserProfile(userId);
        return success(result);
    }

    @GetMapping("/info")
    @Operation(summary = "获取用户详细信息")
    @PreAuthenticated
    public CommonResult<ProfileDetailRespVO> getUserInfo() {
        Long userId = getLoginUserId();
        log.info("获取用户详细信息，用户ID: {}", userId);
        
        ProfileDetailRespVO result = profileService.getUserDetail(userId);
        return success(result);
    }

    @PutMapping("/info")
    @Operation(summary = "更新用户信息")
    @PreAuthenticated
    public CommonResult<Boolean> updateUserInfo(@RequestBody @Valid ProfileUpdateReqVO reqVO) {
        Long userId = getLoginUserId();
        log.info("更新用户信息，用户ID: {}, 请求参数: {}", userId, reqVO);
        
        profileService.updateUserInfo(userId, reqVO);
        return success(true);
    }

    @PostMapping("/avatar")
    @Operation(summary = "上传用户头像")
    @PreAuthenticated
    public CommonResult<ProfileAvatarRespVO> uploadAvatar(
            @Parameter(description = "头像文件", required = true) @RequestParam("avatar") MultipartFile avatar) {
        Long userId = getLoginUserId();
        log.info("上传用户头像，用户ID: {}, 文件名: {}", userId, avatar.getOriginalFilename());
        
        ProfileAvatarRespVO result = profileService.uploadAvatar(userId, avatar);
        return success(result);
    }

}
